//
//  XSJComposeToolBar.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/19.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
enum XSJComposeToolBarButtonType : Int {

    
    //  图片
    case Picture = 0
    //  @
    case Mention = 1
    //  话题
    case Trend = 2
    //  表情
    case Emoticon = 3
    //  add
    case Add = 4

    
    
}
class XSJComposeToolBar: UIStackView {
    
    var didSeletedButtonClosure: ((type: XSJComposeToolBarButtonType)->())?
  
    
    var emoticonButton: UIButton?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpui()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpui() {
    
//        backgroundColor = UIColor.blueColor()
        
        axis = .Horizontal
        distribution = .FillEqually
        
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)


        
    }
    
    private func addChildButton(imageName: String, type: XSJComposeToolBarButtonType) -> UIButton {

    
        let button = UIButton()
        
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
//        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
//        button.adjustsImageWhenHighlighted = false

        
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        addArrangedSubview(button)
        
        return button
    }
    
    @objc private func buttonClick (butotn : UIButton) {
    
        let type = XSJComposeToolBarButtonType(rawValue: butotn.tag)!
        didSeletedButtonClosure?(type: type)
        
        
    }
    
    
    func showEmoticonIcon(isEmoticon: Bool) {
    
        var icon = "compose_emoticonbutton_background"
        
        if isEmoticon {
            icon = "compose_keyboardbutton_background"
        }
        emoticonButton?.setImage(UIImage(named: icon), forState: .Normal)
        emoticonButton?.setImage(UIImage(named: "\(icon)_highlighted"), forState: .Highlighted)

    }
    
    
}
